package com.sharesrc.jdev.wiimotedemo.common.util;

import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JToggleButton;

public class UIUtils {

	private static final GridBagConstraints gbc = new GridBagConstraints();

	public static AbstractButton createButton(String text, boolean enabled, boolean toggleButton) {
		AbstractButton b = null;
		if (toggleButton) {
			b = new JToggleButton(text) {
				/**
				 * 
				 */
				private static final long serialVersionUID = 201308141238L;

				@Override
				public Insets getMargin() {
					if (super.getMargin() != null) {
						return new Insets(super.getMargin().top, 10, super.getMargin().bottom, 10);
					}
					return null;
				}
			};
		} else {
			b = new JButton(text) {
				/**
				 * 
				 */
				private static final long serialVersionUID = 201308141239L;

				@Override
				public Insets getMargin() {
					if (super.getMargin() != null) {
						return new Insets(super.getMargin().top, 10, super.getMargin().bottom, 10);
					}
					return null;
				}
			};
		}
		b.setEnabled(enabled);
		Insets margin = b.getMargin();
		b.setMargin(new Insets(margin.top, 10, margin.bottom, 10));
		return b;
	}

	public static void addComponent(Container owner, Component component, int gridWidth, int gridHeight, int weightX, int weightY, int fill, int insetTop, int insetLeft, int insetBottom, int insetRight, int anchor) {
		if (owner.getLayout() != null) {
			if (!(owner.getLayout() instanceof GridBagLayout)) {
				owner.setLayout(new GridBagLayout());
			}
		}
		gbc.gridwidth = gridWidth;
		gbc.gridheight = gridHeight;
		gbc.weightx = weightX;
		gbc.weighty = weightY;
		gbc.fill = fill;
		gbc.insets.top = insetTop;
		gbc.insets.left = insetLeft;
		gbc.insets.bottom = insetBottom;
		gbc.insets.right = insetRight;
		gbc.anchor = anchor;
		owner.add(component, gbc);
	}
}
